from typing import Dict, Any
from datetime import datetime


class Task:
    def __init__(self, task_id: int, name: str, completed_pomodoros: int = 0, created_at: str = None):
        self.id = task_id
        self.name = name
        self.completed_pomodoros = completed_pomodoros
        self.created_at = created_at or self._get_current_time()
    
    def to_dict(self) -> Dict[str, Any]:
        """将任务对象转换为字典，用于存储"""
        return {
            "id": self.id,
            "name": self.name,
            "completed_pomodoros": self.completed_pomodoros,
            "created_at": self.created_at
        }
    
    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> 'Task':
        """从字典创建任务对象"""
        return cls(
            task_id=data["id"],
            name=data["name"],
            completed_pomodoros=data["completed_pomodoros"],
            created_at=data["created_at"]
        )
    
    def increment_pomodoro(self):
        """增加番茄钟计数"""
        self.completed_pomodoros += 1
    
    def _get_current_time(self):
        """获取当前时间字符串"""
        return datetime.now().isoformat()